MediaComposer Example
This example demonstrates how to use MediaComposer to compose a final video from video, image, and audio sources, and export it to the script directory.
The workflow covered in this example includes:
- Picking an audio file
- Picking an image
- Picking a video
- Building a visual timeline (video + image)
- Inserting audio at a specific time
- Exporting the composed video
Example Code
Timeline Breakdown
Visual Timeline (videoItems)
- The first
VideoItemis a full video clip - The second
VideoItemis an image displayed for 5 seconds - All
videoItemsare concatenated in strict order - Final video duration = video duration + 5 seconds
Audio Timeline (audioClips)
- The audio starts playing at 5 seconds on the final timeline
- When
atis omitted, audio clips are appended sequentially - Audio does not affect the final video duration
Export Result
exportPath: the full output file pathduration: the total video duration (derived fromvideoItems)
Common Errors and Edge Cases
1. ImageClip without duration
Issue:
- Images have no intrinsic duration
- Omitting
durationwill cause composition to fail
Solution:
- Always provide an explicit
MediaTimeduration
2. Using raw numbers instead of MediaTime
Correct usage:
All time values in MediaComposer must be represented by MediaTime.
3. Mixed timescales causing precision issues
Issue:
- Different media sources may use different timescales
- This can lead to rounding errors during trimming, fades, or alignment
Recommendation:
- Use a consistent
preferredTimescale(e.g. 600) - Convert external times using
convertScalewhen needed
4. Audio extending beyond the video duration
Behavior:
- Audio that exceeds the end of the video does not extend the final duration
- Any audio beyond the video end is automatically truncated
5. Unexpected audio balance when mixing original and external audio
Cause:
- By default, original video audio and external audio are mixed together
- Without ducking, dialogue may be masked by background music
Audio Ducking Behavior
What is Ducking
Ducking refers to:
Automatically lowering the volume of external audio (e.g. background music) when original video audio (e.g. dialogue) is present.
Ducking Configuration
Parameters
-
enabled Enables or disables ducking (default:
true) -
duckedVolume Target volume for external audio during ducking (0…1)
-
attackSeconds Ramp-down duration before original audio starts
-
releaseSeconds Ramp-up duration after original audio ends
Conditions for Ducking to Apply
Ducking is applied only when all of the following are true:
VideoClip.keepOriginalAudio === true- At least one external
AudioClipexists exportOptions.ducking.enabled !== false
Audio Mixing Rules Summary
-
Original Video Audio
- Included only when
keepOriginalAudiois set totrue
- Included only when
-
External Audio
- Can be positioned or appended sequentially
- Supports per-clip
volume,fade, and looping
-
Final Mix
- All audio sources are mixed into a single output track
- Audio never changes the final video duration
- Ducking is applied automatically during mixing
